package com.person.service.impl;

import com.person.model.entity.Result;
import com.person.service.OpenFilePathService;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Service
public class OpenFilePathServiceImpl implements OpenFilePathService {

    @Override
    public Result openFilePath(String filePath) {
        System.setProperty("java.awt.headless", "false");
        Path path = Paths.get(filePath);
        if (!Files.exists(path)) {
            try {
                Files.createDirectories(path);
            } catch (IOException e) {
                return Result.error("请检查归档路径");
            }
        }
        File folder = new File(filePath);
        Desktop desktop = Desktop.getDesktop();

        // 打开文件夹
        try {
            desktop.open(folder);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return Result.success();
    }
}
